package com.green.javagreen.controller;

import com.green.javagreen.bean.AdminUserBean;
import com.green.javagreen.bean.QuestionBean;
import com.green.javagreen.bean.ResultBean;
import com.green.javagreen.injections.CurrentUser;
import com.green.javagreen.injections.PermissionCheck;
import com.green.javagreen.service.QuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * email:brioal@foxmail.com
 * github:https://github.com/Brioal
 */
@Api(description = "QuestionBean管理")
@RestController
@RequestMapping("/question")
public class QuestionController {

    private QuestionService questionService;


    @Autowired
    public void setQuestionService(QuestionService questionService) {
        this.questionService = questionService;
    }

    @ApiOperation(value = "获取列表")
    @PostMapping
    @PermissionCheck(ADMIN_ALLOW = true)
    public ResultBean list(@RequestBody QuestionBean questionBean) {
        Page<QuestionBean> page = questionService.find(questionBean);
        return ResultBean.returnPage(page);
    }

    @ApiOperation(value = "保存 新增/修改")
    @PutMapping
    @PermissionCheck(ADMIN_ALLOW = true)
    public ResultBean save(@RequestBody QuestionBean questionBean) {
        questionService.save(questionBean);
        return ResultBean.returnSuccess(null);
    }


    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    @PermissionCheck(ADMIN_ALLOW = true)
    public ResultBean delete(@PathVariable int id) {
        questionService.delete(id);
        return ResultBean.returnSuccess(null);
    }


    @ApiOperation(value = "创建问卷")
    @PostMapping("/create")
    @PermissionCheck(ADMIN_ALLOW = true)
    public ResultBean create(@CurrentUser AdminUserBean adminUserBean, @RequestBody QuestionBean questionBean) {
        questionBean.setAdmin(adminUserBean);
        questionBean = questionService.save(questionBean);
        return ResultBean.returnSuccess(questionBean);
    }

}
